//
//  BQSaoYiSaoViewController.m
//  BeeQuick
//
//  Created by apple on 16/11/19.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQSaoYiSaoViewController.h"

#import <AVFoundation/AVFoundation.h>

#define containerViewHeight 170
@interface BQSaoYiSaoViewController ()<AVCaptureMetadataOutputObjectsDelegate>
    
// 会话
@property (nonatomic, strong)AVCaptureSession *session;
 
// 定时器
@property (nonatomic, strong) CADisplayLink *link;

    
// 扫码视图
@property (nonatomic, weak)UIImageView  *qrcode_scanline_qrcodeView;
// 容器视图
@property (nonatomic, weak) UIView *containerView;
// 扫码视图的头部间距
@property (nonatomic, assign) CGFloat qrcode_scanline_qrcodeViewTop;

@end

@implementation BQSaoYiSaoViewController

    
// 懒加载定时器
- (CADisplayLink *)link
{
    if (_link == nil) {
        //添加定时器
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeImageView)];
    }
       return _link;
    }
    
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    // 设置界面
    [self setupUI];
    
    // 设置初始值
    self.qrcode_scanline_qrcodeViewTop = -containerViewHeight;
}
    
    
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        NSLog(@"%@",metadataObjects.lastObject);
        
        //停止扫描
        [self.session stopRunning];
    }


// 监听方法
- (void)changeImageView
{
    self.qrcode_scanline_qrcodeViewTop +=1;
    
    [self.qrcode_scanline_qrcodeView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(_containerView).offset(_qrcode_scanline_qrcodeViewTop);
    }];
    
    if (_qrcode_scanline_qrcodeViewTop >  containerViewHeight) {
        _qrcode_scanline_qrcodeViewTop = -containerViewHeight;
    }
}
    
    //在view将要显示的时候,添加定时器
- (void)viewWillAppear:(BOOL)animated
{
        [super viewWillAppear:animated];
        
        [self setUpSaoYiSao];
        
        [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
    
    //在view消失的时候 删除定时器
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
        
    [self.link invalidate];
    self.link = nil;        
}

#pragma  mark 设置二维码内容
- (void)setUpSaoYiSao
{
    //1.创建输入对象  获取摄像头
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
    //2.创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
    //3.创建回话 连接输入和输出
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    if ([session canAddInput:input]) {
        [session addInput:input];
    }
    if ([session canAddOutput:output]) {
        
        [session addOutput:output];
    }
    //4.设置输出的代理和输出类型
    
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //5.创建图层
    
    AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
    
    layer.frame = self.view.layer.bounds;
    // 插入
    [self.view.layer insertSublayer:layer atIndex:0];
    //6.开始扫描
    [session startRunning];
    
    self.session = session;
  
}

#pragma mark 设置界面
- (void)setupUI
{
    // 1.添加控件
    UIView *containerView = [[UIView alloc]init];
//    containerView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:containerView];
    

    UIImageView *qrcode_borderView = [[UIImageView alloc]init];
    qrcode_borderView.image = [UIImage imageNamed:@"qrcode_border"];
    [self.view addSubview:qrcode_borderView];
    
    UIImageView *qrcode_scanline_qrcodeView = [[UIImageView alloc]init];
    qrcode_scanline_qrcodeView.image = [UIImage imageNamed:@"qrcode_scanline_qrcode"];
    [self.view addSubview:qrcode_scanline_qrcodeView];
    
    // 设置约束
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
        make.height.mas_equalTo(containerViewHeight);
        make.width.mas_equalTo(218);
    }];

    [qrcode_borderView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(containerView);
    }];
    
    [qrcode_scanline_qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(containerView);
        make.height.equalTo(containerView);
        make.top.equalTo(containerView).offset(-containerViewHeight);
    }];
    
    // 记录属性
    self.qrcode_scanline_qrcodeView = qrcode_scanline_qrcodeView;
    self.containerView = containerView;
    self.containerView.clipsToBounds = YES;
}

@end
